package com.sk.weichat.ui.me;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.sk.weichat.AppConstant;
import com.sk.weichat.R;
import com.sk.weichat.bean.Friend;
import com.sk.weichat.broadcast.CardcastUiUpdateUtil;
import com.sk.weichat.broadcast.MsgBroadcast;
import com.sk.weichat.db.dao.AreasDao;
import com.sk.weichat.db.dao.FriendDao;
import com.sk.weichat.helper.DialogHelper;
import com.sk.weichat.ui.base.BaseActivity;
import com.sk.weichat.ui.message.single.SetLabelActivity;
import com.sk.weichat.util.AppUtils;
import com.sk.weichat.util.ToastUtil;
import com.sk.weichat.view.ClearEditText;
import com.sk.weichat.view.MaxTextTwoLengthFilter;
import com.xuan.xuanhttplibrary.okhttp.HttpUtils;
import com.xuan.xuanhttplibrary.okhttp.callback.BaseCallback;
import com.xuan.xuanhttplibrary.okhttp.result.ObjectResult;
import com.xuan.xuanhttplibrary.okhttp.result.Result;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import okhttp3.Call;

/**
 * 修改昵称
 * create by swy 2019-06-24
 */
public class SetNicknameActivity extends BaseActivity implements View.OnClickListener {

    private ClearEditText mEditNickname;
    private TextView mTvEditNum;
    TextView tvRight;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_nickname);
        initActionBar();
        initView();

    }

    private void initActionBar() {
        getSupportActionBar().hide();
        findViewById(R.id.mergerStatus).setBackgroundColor(ContextCompat.getColor(this,R.color.white));
        TextView tvTitle = (TextView) findViewById(R.id.tv_title_center);
        tvTitle.setText(R.string.nickname);
        tvRight = (TextView) findViewById(R.id.tv_title_right);
        tvRight.setText(R.string.finish);
        tvRight.setTextColor(ContextCompat.getColor(this,R.color.deep_purple));
        tvRight.setOnClickListener(this);
        findViewById(R.id.iv_title_left).setOnClickListener(this);
    }

    private void initView() {
        mEditNickname=findViewById(R.id.edit_nickname);
        mTvEditNum=findViewById(R.id.tv_edit_num);


        mEditNickname.setFilters(new InputFilter[]{new MaxTextTwoLengthFilter(24)});
        mEditNickname.addTextChangedListener(new MyTextWatcher());
        String nickname = getIntent().getStringExtra("nickname");
        mEditNickname.setText(nickname);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_title_left:
                finish();
                break;

            case R.id.tv_title_right:
                Intent intent = new Intent();
                intent.putExtra("nickname",replaceBlank(mEditNickname.getText().toString()));
                setResult(RESULT_OK, intent);
                finish();
                break;
        }
    }
    public static String replaceBlank(String src) {
        String dest = "";
        if (src != null) {
            Pattern pattern = Pattern.compile("\t|\r|\n|\\s*");
            Matcher matcher = pattern.matcher(src);
            dest = matcher.replaceAll("");
        }
        return dest;
    }

    private class MyTextWatcher implements TextWatcher {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (s.length()==0){
                mTvEditNum.setText("0/24");
            }else {
                mTvEditNum.setText(AppUtils.judgeTextLength(s.toString()) +"/24");
            }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    }
}
